package com.china08.yxyapi.service.yxyresource.statistics.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;

import com.china08.yxyapi.entity.es.ElasticYxyResResource;
import com.china08.yxyapi.service.yxyresource.statistics.StatisticsWeiKeService;
@Service
public class StatisticsWeiKeServiceImpl implements StatisticsWeiKeService {
	@Autowired
	private ElasticsearchTemplate et;
	@Override
	public List<Long> statisticsResourceCount(String districtCode, Integer schoolBureauLevel) {
		BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
		TermQueryBuilder builderType = new TermQueryBuilder("typeId", "yx14");
		boolQueryBuilder.must(builderType);
		
		TermQueryBuilder builderTypestauts = new TermQueryBuilder("isDelete", 0);
		boolQueryBuilder.must(builderTypestauts);
		SearchQuery query = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder).build();

		// 查询出来的总数量
		long allCount = et.count(query,ElasticYxyResResource.class);
		List<Long> longCount = new ArrayList<Long>();
		longCount.add(allCount);

		// 区域资源数量 区域字段编码
		
		BoolQueryBuilder boolQueryBuilder1 = new BoolQueryBuilder();
		TermQueryBuilder builderType1 = new TermQueryBuilder("typeId", "yx14");
		boolQueryBuilder1.must(builderType1);
		
		TermQueryBuilder builderTypestauts1 = new TermQueryBuilder("isDelete", 0);
		boolQueryBuilder1.must(builderTypestauts1);
		TermQueryBuilder builderDel =null;
		if (schoolBureauLevel == 0) {
			builderDel = new TermQueryBuilder("districtCode", districtCode);
			boolQueryBuilder1.must(builderDel);
		}else if(schoolBureauLevel == 1){
			builderDel = new TermQueryBuilder("cityCode", districtCode);
			boolQueryBuilder1.must(builderDel);
		}else if (schoolBureauLevel == 2) {
			builderDel = new TermQueryBuilder("provinceCode", districtCode);
			boolQueryBuilder1.must(builderDel);
		}
		boolQueryBuilder.must(builderDel);
		SearchQuery	query2 = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder1).build();
		long areaCount = et.count(query2,ElasticYxyResResource.class);
		longCount.add(areaCount);

		
		BoolQueryBuilder boolQueryBuilder2 = new BoolQueryBuilder();
		TermQueryBuilder builderType2 = new TermQueryBuilder("typeId", "yx14");
		boolQueryBuilder2.must(builderType2);
		
		TermQueryBuilder builderTypestauts2 = new TermQueryBuilder("isDelete", 0);
		boolQueryBuilder2.must(builderTypestauts2);
		
		if (schoolBureauLevel == 0) {
			builderDel = new TermQueryBuilder("districtCode", districtCode);
			boolQueryBuilder2.must(builderDel);
		}else if(schoolBureauLevel == 1){
			builderDel = new TermQueryBuilder("cityCode", districtCode);
			boolQueryBuilder2.must(builderDel);
		}else if (schoolBureauLevel == 2) {
			builderDel = new TermQueryBuilder("provinceCode", districtCode);
			boolQueryBuilder2.must(builderDel);
		}
		// 统计近30天新增加的资源数量 uploadTime
		Date currentDate = new Date();
		// 30天的毫秒数
		long threeDate = 2592000000L;
//		Date date = new Date(currentDate.getTime() - threeDate);
		// TermQueryBuilder builderDel2 = new TermQueryBuilder("uploadTime",
		// date);
		// boolQueryBuilder.
		//
		RangeQueryBuilder rangeDate = new RangeQueryBuilder("uploadTime");
//	    rangeDate.format(date); // 查询格式    （先试下如果不行的话，在加下 格式） 
		// 
//		rangeDate.gt(date);
		
		rangeDate.gt(currentDate.getTime() - threeDate);
		boolQueryBuilder2.filter(rangeDate);
		SearchQuery query3=new NativeSearchQueryBuilder().withQuery(boolQueryBuilder2).build();
		
		
		System.out.println("==================="+boolQueryBuilder.toString());
		long nearCount = et.count(query3,ElasticYxyResResource.class);
		longCount.add(nearCount);
		return longCount;
	}
}
